package com.gzhryc.net_marketing.cloud.goods.services;

import com.gzhryc.common.logging.Logger;
import com.gzhryc.jdbc.Conditions;
import com.gzhryc.jdbc.MultiDBTools;
import com.gzhryc.net_marketing.cloud.goods.dao.db.CloudGoodsSpaceAuditItem;
import com.gzhryc.net_marketing.cloud.MarketConstants;
import com.gzhryc.net_marketing.cloud.goods.dao.CloudGoodsSpaceAuditItemDao;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

public class CloudGoodsSpaceAuditItemService {

    static Logger log = Logger.getLogger(CloudGoodsSpaceAuditItemService.class);

    public static CloudGoodsSpaceAuditItemService self() {
        return MultiDBTools.getService(MarketConstants.JDBC_KEY,CloudGoodsSpaceAuditItemService.class, true);
    }

    CloudGoodsSpaceAuditItemDao dao;

    public CloudGoodsSpaceAuditItemService(String jdbcKey) {
        this.dao = new CloudGoodsSpaceAuditItemDao(jdbcKey);
    }

    public List<CloudGoodsSpaceAuditItem> findByApplyId(Long applyId){
        if(applyId != null){
            Conditions conditions = new Conditions(CloudGoodsSpaceAuditItem.class);
            conditions.and().is("applyId",applyId);

            try {
                return dao.findByCondition(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return null;
    }

    public Long countByApplyId(Long applyId){
        if(applyId != null){
            Conditions conditions = new Conditions(CloudGoodsSpaceAuditItem.class);
            conditions.and().is("applyId",applyId);

            try {
                return dao.count(conditions);
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return 0L;
    }

    public boolean add(Long applyId,List<CloudGoodsSpaceAuditItem> spaceAuditItemList){
        if(applyId != null && spaceAuditItemList != null && spaceAuditItemList.size()>0){
            for(CloudGoodsSpaceAuditItem item : spaceAuditItemList){
                item.setApplyId(applyId);
                item.setCreateDate(new Date());
            }

            try {
                return dao.insert(spaceAuditItemList) > 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }

    /**
     * 删除申请
     * @param applyId
     * @return
     */
    public boolean deleteByApplyId(Long applyId){
        if(applyId != null){
            Conditions conditions = new Conditions(CloudGoodsSpaceAuditItem.class);
            conditions.and().is("applyId",applyId);

            try {
                return dao.deleteByCondition(conditions) >= 0;
            } catch (SQLException e) {
                log.error(e.getMessage(),e);
            }
        }
        return false;
    }
}
